package study.chapter03.thread.sync;

public class TestThread1 {
    public static void main(String[] args) {

        MyThread myThread = new MyThread();
        new Thread(myThread, "售票员A").start();
        new Thread(myThread, "售票员B").start();
        new Thread(myThread, "售票员C").start();

    }
}

class MyThread implements Runnable {
    private int ticket = 5;

    //卖票方法
    public synchronized boolean sale(){
        if (ticket > 0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "卖票：ticket = " + this.ticket--);
            return true;
        }else {
            System.out.println("========= 票卖光了 ========");
            return false;
        }

    }

    @Override
    public void run() {
        while (sale()){
            ;
        }
    }
}
